package com.eedi.framework.mail.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.mail.controller.sys.vo.template.MailTemplateResp;
import com.eedi.framework.mail.controller.sys.vo.template.MailTemplatePageReq;
import com.eedi.framework.mail.controller.sys.vo.template.MailTemplateSaveReq;
import com.eedi.framework.mail.controller.sys.vo.template.MailTemplateSimpleResp;
import com.eedi.framework.mail.dal.dataobject.SysMailTemplateDO;
import com.eedi.framework.mail.service.SysMailTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 邮件模版")
@RestController
@RequestMapping("/mail-template")
public class MailTemplateController {

    @Resource
    private SysMailTemplateService mailTempleService;
//    @Resource
//    private MailSendService mailSendService;

    @PostMapping("/create")
    @Operation(summary = "创建邮件模版")
    @PreAuthorize("@ss.hasPermission('sys:mail-template:create')")
    public CommonResult<String> createMailTemplate(@Valid @RequestBody MailTemplateSaveReq createReqVO){
        return success(mailTempleService.createMailTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改邮件模版")
    @PreAuthorize("@ss.hasPermission('sys:mail-template:update')")
    public CommonResult<Boolean> updateMailTemplate(@Valid @RequestBody MailTemplateSaveReq updateReqVO){
        mailTempleService.updateMailTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除邮件模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:mail-template:delete')")
    public CommonResult<Boolean> deleteMailTemplate(@RequestParam("id") String id) {
        mailTempleService.deleteMailTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得邮件模版")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:mail-template:get')")
    public CommonResult<MailTemplateResp> getMailTemplate(@RequestParam("id") String id) {
        SysMailTemplateDO template = mailTempleService.getMailTemplate(id);
        return success(BeanUtils.toBean(template, MailTemplateResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得邮件模版分页")
    @PreAuthorize("@ss.hasPermission('sys:mail-template:query')")
    public CommonResult<PageResult<MailTemplateResp>> getMailTemplatePage(@Valid MailTemplatePageReq pageReqVO) {
        PageResult<SysMailTemplateDO> pageResult = mailTempleService.getMailTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MailTemplateResp.class));
    }

    @GetMapping({"/list-all-simple", "simple-list"})
    @Operation(summary = "获得邮件模版精简列表")
    public CommonResult<List<MailTemplateSimpleResp>> getSimpleTemplateList() {
        List<SysMailTemplateDO> list = mailTempleService.getMailTemplateList();
        return success(BeanUtils.toBean(list, MailTemplateSimpleResp.class));
    }

//    @PostMapping("/send-mail")
//    @Operation(summary = "发送短信")
//    @PreAuthorize("@ss.hasPermission('sys:mail-template:send-mail')")
//    public CommonResult<Long> sendMail(@Valid @RequestBody MailTemplateSendReqVO sendReqVO) {
//        return success(mailSendService.sendSingleMailToAdmin(sendReqVO.getMail(), getLoginUserId(),
//                sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
//    }

}
